from calendar import isleap
import datetime
import os
import random
import sys
sys.path.append ('DataMiningPackage')
sys.path.append ('data')
sys.path.append ('model')
sys.path.append ('UIPackage')
from threading import Thread
import kivy
from kivy.app import App
from kivy.core.text import LabelBase
from kivy.core.window import Window,WindowBase
from kivy.uix.screenmanager import ScreenManager,Screen,FadeTransition
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
from kivy.animation import Animation
from kivy.config import Config
import data
from data import Data
import Func
from Func import EmotionAnalysis as EA
from Func import DataManagement as DM
import data
import model
from model import ATrainModel as AM
from model import TTrainModel as TM
import UI.Screens
from UI.Screens import FirstUseScreen
from UI.Screens import LoginScreen
from UI.Screens import RegisterScreen
from UI.Screens import ChangePasswordScreen
from UI.Screens import MainScreen
from UI.Screens import FMAddScreen
from UI.Screens import FMReadScreen
from UI.Screens import EAAnalysisScreen
from UI.Screens import EAResultScreen
from UI.Screens import EAWordsInputScreen
from UI.Screens import DMInput1Screen
from UI.Screens import DMInput2Screen
from UI.Screens import DMSelectScreen
from UI.Screens import DiaryAddScreen
from UI.Screens import DiaryReadScreen
from UI.Screens import OpeningScreen
from UI.Screens import LoadingScreen
from UI.Screens import HelpScreen
from UI.Screens import QuestionScreen
import PublicData


LabelBase.register('Roboto','./fonts/sej.ttf') #默认字体
Config.set('graphics', 'resizable', False)
Config.write()


Window.top = 25
Window.left = 0
Window.size = (1200,600)


class MainApp (App):

    def __init__(self,PD,**kwargs):
        super().__init__(**kwargs)

        self.PD = PD

        self.UserData = PD.UserData #用户信息
        self.OM = Data.ObjMemory() #文件读取器


    def InitFU (self): #初始化首次使用界面
        S_FU = FirstUseScreen.FirstUseScreen(self.PD,name='firstuse')
        return S_FU


    def InitLG (self): #初始化登录界面
        S_LG = LoginScreen.LoginScreen(self.PD,name='login')
        S_LG.ids.Button_identity.bind(on_release=S_LG.dropdown_identity.open)
        S_LG.dropdown_identity.bind(on_select=lambda instance, x: \
        setattr(S_LG.ids.Button_identity, 'text', x))
        return S_LG


    def InitRG (self): #初始化注册界面
        S_RG = RegisterScreen.RegisterScreen(self.PD,name='register')
        S_RG.ids.Button_identity.bind(on_release=S_RG.dropdown_identity.open)
        S_RG.dropdown_identity.bind(on_select=lambda instance, x: \
        setattr(S_RG.ids.Button_identity, 'text', x))
        return S_RG


    def InitMain (self): #初始化主界面
        S_Main = MainScreen.MainScreen(self.PD,name='main')
        return S_Main

    
    def InitPC (self): #初始化修改密码界面
        S_CP = ChangePasswordScreen.ChangePasswordScreen(self.PD,name='passwordchange')
        S_CP.ids.Button_identity.bind(on_release=S_CP.dropdown_identity.open)
        S_CP.dropdown_identity.bind(on_select=lambda instance, x: \
        setattr(S_CP.ids.Button_identity, 'text', x))
        return S_CP


    def InitFMA (self): #初始化新建便签界面
        S_FMA = FMAddScreen.FMAddScreen(self.PD,name='addmemo')
        return S_FMA


    def InitFMR (self): #初始化查看便签界面
        S_FMR = FMReadScreen.FMReadScreen(self.PD,name='readmemo')
        return S_FMR

    
    def InitEAA (self): #初始化情绪分析界面
        S_EAA = EAAnalysisScreen.EAAnalysisScreen (PD,name='analysis')
        S_EAA.ids.Button_A1.bind(on_release=S_EAA.A1.open)
        S_EAA.ids.Button_A2.bind(on_release=S_EAA.A2.open)
        S_EAA.ids.Button_A3.bind(on_release=S_EAA.A3.open)
        S_EAA.ids.Button_A4.bind(on_release=S_EAA.A4.open)
        S_EAA.ids.Button_A5.bind(on_release=S_EAA.A5.open)
        S_EAA.A1.bind(on_select=lambda instance, x:setattr(S_EAA.ids.Button_A1,'text', x))
        S_EAA.A2.bind(on_select=lambda instance, x:setattr(S_EAA.ids.Button_A2,'text', x))
        S_EAA.A3.bind(on_select=lambda instance, x:setattr(S_EAA.ids.Button_A3,'text', x))
        S_EAA.A4.bind(on_select=lambda instance, x:setattr(S_EAA.ids.Button_A4,'text', x))
        S_EAA.A5.bind(on_select=lambda instance, x:setattr(S_EAA.ids.Button_A5,'text', x))
        return S_EAA

    
    def InitEAW (self):
        S_EAW = EAWordsInputScreen.EAWordsInputScreen(PD,name='wordsinput')
        return S_EAW

    
    def InitEAR (self):
        S_EAR = EAResultScreen.EAResultScreen (self.PD,name='result')
        return S_EAR

    
    def InitDMS (self):
        S_DMS = DMSelectScreen.DMSelectScreen (self.PD,name='select')
        return S_DMS

    
    def InitDMI1 (self): #初始化查看便签界面
        S_DMI1 = DMInput1Screen.DMInput1Screen(name='input1')
        
        S_DMI1.ids.Button_emotion.bind(on_release=S_DMI1.dropdown_emotion.open)
        S_DMI1.ids.Button_hidden.bind(on_release=S_DMI1.dropdown_hidden.open)
        S_DMI1.ids.Button_activating.bind(on_release=S_DMI1.dropdown_activating.open)
        S_DMI1.ids.Button_slv1.bind(on_release=S_DMI1.dropdown_solution1.open)
        S_DMI1.ids.Button_slv2.bind(on_release=S_DMI1.dropdown_solution2.open)
        S_DMI1.ids.Button_slv3.bind(on_release=S_DMI1.dropdown_solution3.open)

        S_DMI1.dropdown_emotion.\
        bind(on_select=lambda instance, x:setattr(S_DMI1.ids.Button_emotion,'text', x))
        S_DMI1.dropdown_hidden.\
        bind(on_select=lambda instance, x:setattr(S_DMI1.ids.Button_hidden,'text', x))
        S_DMI1.dropdown_activating.\
        bind(on_select=lambda instance, x:setattr(S_DMI1.ids.Button_activating,'text', x))
        S_DMI1.dropdown_solution1.\
        bind(on_select=lambda instance, x:setattr(S_DMI1.ids.Button_slv1,'text', x))
        S_DMI1.dropdown_solution2.\
        bind(on_select=lambda instance, x:setattr(S_DMI1.ids.Button_slv2,'text', x))
        S_DMI1.dropdown_solution3.\
        bind(on_select=lambda instance, x:setattr(S_DMI1.ids.Button_slv3,'text', x))
        return S_DMI1

    
    def InitDMI2 (self):
        S_DMI2 = DMInput2Screen.DMInput2Screen(self.PD,name='input2')
        return S_DMI2

    
    def InitDA (self):
        S_DA = DiaryAddScreen.DiaryAddScreen(self.PD,name='adddiary')
        return S_DA

    
    def InitDR (self):
        S_DR = DiaryReadScreen.DiaryReadScreen(self.PD,name='readdiary')
        return S_DR

    
    def InitOP (self): #开场动画
        S_OP = OpeningScreen.OpeningScreen(self.PD,name='opening')
        return S_OP

    
    def InitLD (self): #加载界面
        S_LD = LoadingScreen.LoadingScreen (self.PD,name='loading')
        return S_LD

    
    def InitHP (self): #帮助界面
        S_HP = HelpScreen.HelpScreen (name='help')
        return S_HP

    
    def InitQuestion (self): #提问界面
        S_Question = QuestionScreen.QuestionScreen (self.PD,name='question')
        S_Question.ids.Button_type.bind(on_release=S_Question.dropdown_type.open)
        S_Question.dropdown_type.bind(on_select=lambda instance, x: \
        setattr(S_Question.ids.Button_type, 'text', x))
        return S_Question


    def build(self):
        sm = ScreenManager()
        sm.transition = FadeTransition()
        S_FU = self.InitFU()
        S_LG = self.InitLG()
        S_RG = self.InitRG()
        S_CP = self.InitPC()
        S_FMA = self.InitFMA()
        S_Main = self.InitMain()
        S_FMR = self.InitFMR()
        S_EAA = self.InitEAA()
        S_EAW = self.InitEAW()
        S_EAR = self.InitEAR()
        S_DMS = self.InitDMS()
        S_DMI1 = self.InitDMI1()
        S_DMI2 = self.InitDMI2()
        S_DA = self.InitDA()
        S_DR = self.InitDR()
        S_OP = self.InitOP()
        S_LD = self.InitLD()
        S_HP = self.InitHP()
        S_Question = self.InitQuestion()
        self.title = '陌上花开 V1.1.0'
        self.icon = './UI/icon/logo.png'

        sm.add_widget (S_FU)
        sm.add_widget (S_LG)
        sm.add_widget (S_RG)
        sm.add_widget (S_CP)
        sm.add_widget (S_Main)
        sm.add_widget (S_FMA)
        sm.add_widget (S_FMR)
        sm.add_widget (S_EAA)
        sm.add_widget (S_EAW)
        sm.add_widget (S_EAR)
        sm.add_widget (S_DMS)
        sm.add_widget (S_DMI1)
        sm.add_widget (S_DMI2)
        sm.add_widget (S_DA)
        sm.add_widget (S_DR)
        sm.add_widget (S_OP)
        sm.add_widget (S_LD)
        sm.add_widget (S_HP)
        sm.add_widget (S_Question)

        sm.current = 'opening'

        return sm


if __name__ == '__main__':
    PD = PublicData.PublicData ()
    MainApp(PD).run()